package com.ymatou.iapi.productmanager.testcase;

import com.ymatou.iapi.productmanager.parameter.SetMemberProductBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmanager.service.SetMemberProductCall;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.EnvSetup;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 全球优选设置会员商品
 * FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SetMemberProduct{
	private static SetMemberProductBean setmemberproductBean;
	private static SetMemberProductCall setmemberproductCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("globalSellerId"));
	YmtProductsIWapper ymtProductsIWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("全球优选设置会员商品");
	}

	@Before
	public void caseUp() {
		setmemberproductBean=new SetMemberProductBean();
		setmemberproductCall=new SetMemberProductCall();
		ymtProductsIWapper=new YmtProductsIWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(setmemberproductCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetMemberProduct_002() {
		Logger.start(true,"普通商品设置失败");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV2.initAddSellerProductRequest(3383);
			String productId1 = ProductManagerCallServiceV2.addNewProductOnSale(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
			setmemberproductBean.setMember(true);
			setmemberproductBean.setProductId(productId1);
			setmemberproductBean.setUserId(sellerId);
			setmemberproductCall.setData(setmemberproductBean);
			setmemberproductCall.callService();			
			Logger.verifyEquals("201", setmemberproductCall.getString("Code"), "验证Code");
			Map<String,Object> mapInSql = ymtProductsIWapper.selectProductByProductIdforMap(productId1).get(0);
			Logger.verifyEquals(false, mapInSql.get("bForVip"), "验证Member");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetMemberProduct_003() {
		Logger.start(true,"反复设置全球优选设置会员商品成功");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV2.initAddSellerGlobalPspProductRequest(sellerId);
			String productId1 = ProductManagerCallServiceV2.addNewProductOnSale(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
			setmemberproductBean.setMember(true);
			setmemberproductBean.setProductId(productId1);
			setmemberproductBean.setUserId(sellerId);
			setmemberproductCall.setData(setmemberproductBean);
			setmemberproductCall.callService();			
			Logger.verifyEquals("200", setmemberproductCall.getString("Code"), "验证Code");
			Map<String,Object> mapInSql = ymtProductsIWapper.selectProductByProductIdforMap(productId1).get(0);
			Logger.verifyEquals(setmemberproductBean.getMember(), mapInSql.get("bForVip"), "验证Member");
			setmemberproductBean.setMember(false);
			setmemberproductCall.callService();			
			Logger.verifyEquals("200", setmemberproductCall.getString("Code"), "验证Code");
			mapInSql = ymtProductsIWapper.selectProductByProductIdforMap(productId1).get(0);
			Logger.verifyEquals(setmemberproductBean.getMember(), mapInSql.get("bForVip"), "验证Member");
			setmemberproductBean.setMember(true);
			setmemberproductCall.callService();			
			Logger.verifyEquals("200", setmemberproductCall.getString("Code"), "验证Code");
			mapInSql = ymtProductsIWapper.selectProductByProductIdforMap(productId1).get(0);
			Logger.verifyEquals(setmemberproductBean.getMember(), mapInSql.get("bForVip"), "验证Member");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetMemberProduct_004() {
		Logger.start(true,"不设置Member失败");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV2.initAddSellerGlobalPspProductRequest(sellerId);
			String productId1 = ProductManagerCallServiceV2.addNewProductOnSale(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
//			setmemberproductBean.setMember(true);
			setmemberproductBean.setProductId(productId1);
			setmemberproductBean.setUserId(sellerId);
			setmemberproductCall.setData(setmemberproductBean);
			setmemberproductCall.callService();			
			Logger.verifyEquals("400", setmemberproductCall.getString("Code"), "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetMemberProduct_005() {
		Logger.start(true,"不设置ProductId失败");
		try {
			setmemberproductBean.setMember(true);
//			setmemberproductBean.setProductId(productId1);
			setmemberproductBean.setUserId(sellerId);
			setmemberproductCall.setData(setmemberproductBean);
			setmemberproductCall.callService();			
			Logger.verifyEquals("400", setmemberproductCall.getString("Code"), "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetMemberProduct_006() {
		Logger.start(true,"不设置userid失败");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV2.initAddSellerGlobalPspProductRequest(sellerId);
			String productId1 = ProductManagerCallServiceV2.addNewProductOnSale(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
			setmemberproductBean.setMember(true);
			setmemberproductBean.setProductId(productId1);
//			setmemberproductBean.setUserId(sellerId);
			setmemberproductCall.setData(setmemberproductBean);
			setmemberproductCall.callService();			
			Logger.verifyEquals("400", setmemberproductCall.getString("Code"), "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetMemberProduct_007() {
		Logger.start(true,"userid和商品不匹配失败");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV2.initAddSellerGlobalPspProductRequest(sellerId);
			String productId1 = ProductManagerCallServiceV2.addNewProductOnSale(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
			setmemberproductBean.setMember(true);
			setmemberproductBean.setProductId(productId1);
			setmemberproductBean.setUserId(sellerId+1);
			setmemberproductCall.setData(setmemberproductBean);
			setmemberproductCall.callService();			
			Logger.verifyEquals("201", setmemberproductCall.getString("Code"), "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	////////////////// 忽略case，业务下架 或者业务更改/////////
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_SetMemberProduct_001() {
		Logger.start(true,"全球优选设置会员商品成功");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV2.initAddSellerGlobalPspProductRequest(sellerId);
			String productId1 = ProductManagerCallServiceV2.addNewProductOnSale(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
			setmemberproductBean.setMember(true);
			setmemberproductBean.setProductId(productId1);
			setmemberproductBean.setUserId(sellerId);
			setmemberproductCall.setData(setmemberproductBean);
			setmemberproductCall.callService();
			Logger.verifyEquals("200", setmemberproductCall.getString("Code"), "验证Code");
			Map<String,Object> mapInSql = ymtProductsIWapper.selectProductByProductIdforMap(productId1).get(0);
			Logger.verifyEquals(setmemberproductBean.getMember(), mapInSql.get("bForVip"), "验证Member");
			MessageListBean MessageListBean = MqQueryCallService.initPMongoCrud(productId1);
			List<Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa=joa.stream().filter(j->j.get("ActionType").equals("SetMemberProduct")).collect(Collectors.toList());
			Logger.verifyEquals(1, joa.size(), "验证发送指令SetMemberProduct");
			joa.forEach(ele->{
				if(ele.get("ActionType").equals("SetMemberProduct")) {
					Logger.verifyEquals(productId1, ele.get("ProductId"), "验证productId1");
					//这个指令不会用到SellerId by 文强华 20190802
//		        		Logger.verifyEquals(sellerId, ele.get("SellerId"), "验证SellerId");
				}
				Logger.debug(ele.toString());
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}